package com.chinda.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.chinda.common.annotation.SysLog;
import com.chinda.common.enums.MessageEnum;
import com.chinda.common.exception.RRException;
import com.chinda.common.utils.R;
import com.chinda.modules.sys.dto.SysMenuDTO;
import com.chinda.modules.sys.entity.SysMenuEntity;
import com.chinda.modules.sys.qo.SysMenuQO;
import com.chinda.modules.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;


/**
 * 菜单管理
 *
 * @author chinda
 * @date 2021-06-20 07:18:54
 */
@AllArgsConstructor
@RestController
@RequestMapping("/sysmenus")
@Api(tags = "菜单管理")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    /**
     * 分页
     *
     * @param qo
     * @return
     */
    @SysLog("查询菜单管理分页")
    @PostMapping("/page")
    @ApiOperation(value = "查询分页信息")
    public R page(@ApiParam(value = "qo", name = "查询实例", required = true) @RequestBody SysMenuQO qo) {
        return R.ok().set(sysMenuService.page(qo));
    }

    /**
     * 树
     *
     * @param qo
     * @return
     */
    @SysLog("查询菜单管理树")
    @PostMapping("/tree")
    @ApiOperation(value = "查询菜单树信息")
    public R listWithTree(@ApiParam(value = "qo", name = "查询实例", required = true) @RequestBody SysMenuQO qo) {
        return R.ok().set(sysMenuService.listWithTree(qo));
    }

    /**
     * 获取详情
     *
     * @return
     */
    @SysLog("查询菜单管理详情")
    @GetMapping("/{menuId}")
    @ApiOperation(value = "获取详情" )
    public R info(@PathVariable("menuId")  Long menuId) {
        SysMenuEntity sysMenu = sysMenuService.getById(menuId);
        return R.ok().set(sysMenu);
    }

    /**
     * 保存
     *
     * @return
     */
    @SysLog("保存菜单管理")
    @PostMapping
    @ApiOperation(value = "保存信息")
    public R save(@ApiParam(value = "sysMenuDTO", name = "菜单管理DTO实例", required = true) @RequestBody @Valid SysMenuDTO sysMenuDTO) {
        SysMenuEntity sysMenu = new SysMenuEntity();
        BeanUtil.copyProperties(sysMenuDTO, sysMenu, CopyOptions.create().ignoreNullValue());
        sysMenuService.save(sysMenu);
        return R.ok().set(sysMenu);
    }

    /**
     * 修改全属性
     *
     * @param sysMenuDTO
     * @return
     */
    @SysLog("修改菜单管理全属性")
    @PutMapping("/{menuId}")
    @ApiOperation(value = "修改全属性")
    public R modify(@PathVariable("menuId") Long menuId,
                    @ApiParam(value = "sysMenuDTO", name = "菜单管理DTO实例", required = true) @RequestBody @Valid SysMenuDTO sysMenuDTO) {
        SysMenuEntity sysMenu = new SysMenuEntity();
        BeanUtil.copyProperties(sysMenuDTO, sysMenu, CopyOptions.create().ignoreNullValue());
        sysMenu.setMenuId(menuId);
        if (!sysMenuService.updateById(sysMenu)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(sysMenu);
    }

    /**
     * 修改局部属性
     *
     * @param menuId
     * @param sysMenuDTO
     * @return
     */
    @SysLog("修改菜单管理局部属性")
    @PatchMapping("/{menuId}")
    @ApiOperation(value = "修改局部属性")
    public R patch(@PathVariable("menuId") Long menuId,
                   @ApiParam(value = "sysMenuDTO", name = "菜单管理DTO实例", required = true) @RequestBody @Valid SysMenuDTO sysMenuDTO) {
        SysMenuEntity dbSysMenu = sysMenuService.getById(menuId);
        if (dbSysMenu == null) {
            throw new RRException(MessageEnum.SYS_CHECK_0022.getMsg());
        }
        BeanUtil.copyProperties(sysMenuDTO, dbSysMenu, CopyOptions.create().ignoreNullValue());
        if (!sysMenuService.updateById(dbSysMenu)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(dbSysMenu);
    }

    /**
     * 删除数据
     *
     * @param menuId
     * @return
     */
    @SysLog("删除菜单管理")
    @DeleteMapping("/{menuId}")
    @ApiOperation(value = "删除数据")
    public R remove(@PathVariable("menuId") Long menuId) {
        sysMenuService.removeById(menuId);
        return R.ok();
    }
    /**
     * 批量删除数据
     *
     * @param menuIds
     * @return
     */
    @SysLog("批量删除菜单管理")
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除数据")
    public R remove(@ApiParam(value = "menuIds", name = "menuId集合", required = true) @RequestBody Long [] menuIds) {
        sysMenuService.removeByIds(Arrays.asList(menuIds));
        return R.ok();
    }
}
